package com.greyu.ysj.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.greyu.ysj.authorization.annotation.Authorization;
import com.greyu.ysj.config.Constants;
import com.greyu.ysj.entity.CbtInvest;
import com.greyu.ysj.entity.CbtUserUpdate;
import com.greyu.ysj.entity.User;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtInvestService;
import com.greyu.ysj.service.CbtUserUpdateMapperService;
import com.greyu.ysj.service.InvitationTreeService;
import com.greyu.ysj.service.UserService;
import com.greyu.ysj.utils.InvestFlag;

import com.greyu.ysj.utils.NodeItem;

@Component
@Service
@RestController
@RequestMapping("/invitation")
public class InvitationTreeController {
	
	@Autowired
	private CbtInvestService cbtInvestService;
	@Autowired
	private UserService userService;
	
	@Autowired
	private InvitationTreeService invitationTreeService;
	@Autowired
	private CbtUserUpdateMapperService cbtUserUpdateMapperService;

	private void prcessRecommandInvestByDay() throws IOException{
		//获取所有用户的userId
		/*
		 * 0 : 直接推荐将
		 * 1 : 团队推荐奖励
		 */
		System.out.println("====prcessRecommandInvestByDay====");
		List<User> userIdList=this.userService.selectAllUsers(new User());
		
		Map<Integer, Integer> processMap = new HashMap<Integer, Integer>();
		
		List<InvestFlag> investList = new ArrayList<>();
		
			int childCnt = Constants.DEFAULT_CHILD_MAX;
	        this.invitationTreeService.createNodeList(0, childCnt);
	        //进行更新该节点下所有invest的数据信息
	       
		}
	
	
	/*
	 * 定时更新用户信息的推荐信息
	 */
	private void prcessRecommandInvestRealtime() throws IOException {
		//先进行查找数据用户的数据信息
		System.out.println("=============>>>>process prcessRecommandInvestRealtime");
		CbtUserUpdate tmp = new CbtUserUpdate();
		tmp.setStatus(0);
		List<CbtUserUpdate> usersList = this.cbtUserUpdateMapperService.selectPrcessList(tmp);
		if (usersList.size() <=0) {
			return;
		}
		//进行更新数据的状态--正在处理中
		Map<Integer, Integer> processMap = new HashMap<Integer, Integer>();
		this.cbtUserUpdateMapperService.update(usersList, 1);
		
		for (CbtUserUpdate cbtUserUpdate : usersList) {
			//进行计算每个根节点的信息
			//获取该用户的推荐树
			Integer idss = cbtUserUpdate.getMemberId().intValue();
			Integer rootMemberId = this.invitationTreeService.findRootID(idss);
//			if (processMap.containsKey(rootMemberId)) {
//				continue;
//			}
//			processMap.put(rootMemberId, rootMemberId);
			
			int childCnt = Constants.DEFAULT_CHILD_MAX;
			
			User users = userService.selectByUserId(rootMemberId);

			if (users!=null) {
				 //进行构造对应的推荐树
		        this.invitationTreeService.createOneNodeList(users, 0, childCnt);//
			}
		}
		//将已经处理的数据进行删除掉
		this.cbtUserUpdateMapperService.delete(usersList);

	}
	
	
	
//	@PostMapping(value = "/ssss")
////	@Authorization
//	public ResponseEntity<ResultModel> incomeProcess(@RequestParam("userId") Integer userId) throws IOException {
//		
//		User user = new User();
//		user.setUserId(userId);
//		List<User> list = userService.getAllUsers(1, 1, user);
//		invitationTreeService.createOneNodeList(list.get(0), 0, 16);
//		return new ResponseEntity<>(ResultModel.ok("true"), HttpStatus.OK);
//	}
//	
//	
	
//	/*
//	 * 处理推荐数据
//	 */
//	@Scheduled(cron = "0 0/3 * * * ?")
//	public void updateCbtRecommendationTask() {
//		try {
//			prcessRecommandInvestRealtime();
//			Thread.sleep(1000);
//		}catch (Exception e) {
//			System.out.println(e.getMessage());
//			e.printStackTrace();
//		}
//	}
//	/*
//	 * 处理推荐数据
//	 */
//	@Scheduled(cron = "0 0 0 * * ?")
//   public void rewardProcessTask() {
//       //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "    " + Thread.currentThread().getName() + "    任务一启动");
//		try {
//			prcessRecommandInvestByDay();
//			Thread.sleep(1000);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//			e.printStackTrace();
//		}
//	}
//	
//	
//	
//	
//	
//	@PostMapping(value = "/yyyyy")
////	@Authorization
//	public ResponseEntity<ResultModel> yyyy() throws IOException {
//		
//		List<CbtInvest> allList = cbtInvestService.getAllList();
//		
//		allList.stream().forEach(ss->{
//			synchronized (ss) {
//				cbtInvestService.updateFlag(ss.getId(), 0);
//				User user2 = userService.selectByUserId(ss.getUserId());
//				invitationTreeService.createOneNodeList(user2, 0, 16);
//			
//			}
//				
//			
//		});
//	
//		return new ResponseEntity<>(ResultModel.ok("true"), HttpStatus.OK);
//	}
//	
	
	
	
	
	
	
	}
	

